package com.example.module_mine.login

import android.widget.Toast
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.base.BaseActivity
import com.example.lib_common.utils.RouterPath
import com.example.module_mine.R
import com.example.module_mine.databinding.ActivityLoginBinding
import com.example.module_mine.databinding.LoginFramlayoutBinding
import com.example.module_mine.login.viewmodel.LoginViewModel
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlin.concurrent.thread

@AndroidEntryPoint
@Route(path = RouterPath.LOGIN_UI)
class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_login
    private val mViewModel: LoginViewModel by viewModels()
    lateinit var login_binding: LoginFramlayoutBinding

    @Autowired
    @JvmField
    var un: String = ""

    @Autowired
    @JvmField
    var pw: String = ""
    override fun initData() {
        super.initData()
        //登录回调
        logincallback()


        //注册
        login_binding.btnGotoresgister.setOnClickListener {
            ARouter.getInstance().build("/register/RegisterActivity").navigation()
        }
    }

    private fun logincallback() {
        mViewModel.loginsuccesslivedata.observe(this) {
            thread {
                MMKV.defaultMMKV().encode("userid", it.id)
                MMKV.defaultMMKV().encode("token", "${it.token}")
                MMKV.defaultMMKV().encode("username", it.username)
                MMKV.defaultMMKV().encode("password", it.password)
            }
             Toast.makeText(
                this,
                R.string.login_success,
                Toast.LENGTH_SHORT
            ).show()


            ARouter.getInstance().build("/main/MainActivity").navigation()

        }

        mViewModel.loginfailedlivedata.observe(this) {
            Toast.makeText(this, "" + it, Toast.LENGTH_SHORT).show()
        }
    }

    override fun initView() {
        super.initView()
        searchBindint()
        login_binding.loginUsername.setText(un)
        login_binding.loginPassword.setText(pw)
        //登录
        loginclick()


    }

    private fun loginclick() {
        login_binding.btnLogin.setOnClickListener {

            if (login_binding.loginUsername.text.toString()
                    .isNotEmpty() || login_binding.loginPassword.text.toString().isNotEmpty()
            ) {

                mViewModel.login(
                    login_binding.loginUsername.text.toString(),
                    login_binding.loginPassword.text.toString()
                )
            } else {
                Toast.makeText(this, R.string.user_and_pass_not_null, Toast.LENGTH_SHORT).show()
            }

        }
    }

    private fun searchBindint() {
        login_binding = DataBindingUtil.inflate(
            layoutInflater,
            R.layout.login_framlayout,
            mBinding.loginFramelayout,
            false
        )
        mBinding.loginFramelayout.addView(login_binding.root)
    }

    override fun initListener() {
        super.initListener()
    }
}